class GroupNode extends MyNode{
    constructor(entityType,$state='new',...obj){
        super(`${entityType}Group`,$state,{
            combineType:"or",
            outId:0,
            $faults:new Set(),
            $reasons:new Set(),
            $issues:new Set(),
            $solutions:new Set(),
            frequency:{}
        },...obj);

        if (entityType=="Feature"){
            this.entityType = entityType;
            this.quantum = this.defaultQuantum();
        }
    }
    getText(){
        let sIndex = '';
        if(this.outId!=0)
            sIndex = `<${this.outId}>`;
        return `${GroupNode.getMetaTypeName(this.entityType)}组合${sIndex}:${this.combineType}`;
    }

    static getMetaTypeName(entityType){
        switch (entityType) {
            case 'Feature':
                return "特征";
            case 'Reason':
                return "原因";
            case 'Component':
                return "测点";
        }
        return '';
    }

    getTypeName(){
        return "组合";
    }

    defaultQuantum(){
        if (this.quantum instanceof Object){
            this.quantum = this.quantum.low;
        }
        return this.quantum || 100;
    }

    defaultFrequency(faultId){
        let df = {};
        df[faultId] = 0;
        delete this.frequency["undefined"];
        return $.extend(true,df,this.frequency);
    }

    totalFrequency(){
        let dfs = this.frequency || {};
        return Object.keys(dfs).map(key=>dfs[key]).reduce((x,y)=>eval(x)+eval(y),0);
    }

    getFaults(){
        return _.uniqBy([... this.$faults], f=>f.id);
    }

    getReasons() {
        return [... (this.$reasons || [])]
    }

    getReadonlyTemplate(edge,g){
        return `
 <div class="form-group">
    <label class="col-sm-2 control-label">组合方式</label>
    <p class="col-sm-10 form-control-static">${this.combineType}</p>
 </div>`
    }

    getTemplate(edge,g){
        return `
  <div class="form-group">
    <label class="col-sm-2 control-label">导出ID</label>
    <div class="col-sm-10">
    <input type="text" class="form-control" data-ng-model="graph.getNode('${this.id}').outId" placeholder="用于导出的ID">
    </div>
 </div>
 <div class="form-group">
    <label class="col-sm-2 control-label">组合方式</label>
    <div class="col-sm-10">
    <select class="form-control" data-ng-model="graph.getNode('${this.id}').combineType" data-ng-options="item for item in ['and','or','not']" placeholder="选择一项"></select>
    </div>
 </div>
  <div class="form-group" ng-if="graph.getNode('${this.id}').$is_sub_graph">
    <label class="col-sm-2 control-label">置信度</label>
    <div class="col-sm-10">
    <table class="table table-responsive table-bordered">
    <thead>
        <tr><td>所属故障</td><td>频率</td><td>概率</td></tr>
    </thead>
    <tbody ng-init="graph.getNode('${this.id}').quantum = graph.getNode('${this.id}').defaultQuantum(); graph.getNode('${this.id}').frequency = graph.getNode('${this.id}').defaultFrequency(graph.getRoot().id); ">
        <tr ng-repeat="ft in graph.getNode('${this.id}').getFaults() track by ft.id">
        <td>{{ft.getText()}}</td>
        <td>{{graph.getNode('${this.id}').frequency[ft.id] || 0}}</td>
        <td>{{((graph.getNode('${this.id}').frequency[ft.id] || 0) / graph.getNode('${this.id}').defaultQuantum() * 100).toFixed(2)}}%</td>
        </tr>
    </tbody>
    <tfoot>
        <tr><td>总和</td><td>{{graph.getNode('${this.id}').totalFrequency()}}</td>
        <td>{{(graph.getNode('${this.id}').totalFrequency() / graph.getNode('${this.id}').defaultQuantum() *100).toFixed(2)}}</td></tr>
        <tr><td>可分配</td><td>{{graph.getNode('${this.id}').defaultQuantum() - graph.getNode('${this.id}').totalFrequency()}}</td>
        <td>{{((graph.getNode('${this.id}').defaultQuantum() - graph.getNode('${this.id}').totalFrequency()) / graph.getNode('${this.id}').defaultQuantum() * 100).toFixed(2)}}</td></tr>
    </tfoot>
    </table>
    请输入对当前的故障分配初始频率(应小于等于可分配的数量)：
    <input ng-change="frequencyUpdated('${this.id}')" ng-model="graph.getNode('${this.id}').frequency[graph.getRoot().id]" data-ng-model-options="{updateOn:'default blur',debounce:{default:5000,blur:0}}">
    </div>
  </div>
    
  <div class="form-group" ng-if="graph.getNode('${this.id}').$is_sub_graph2">
    <label class="col-sm-2 control-label">对原因的置信度</label>
    <div class="col-sm-10">
    <table class="table table-responsive table-bordered">
    <thead>
        <tr><td>所属原因</td><td>置信度</td></tr>
    </thead>
    <tbody>
        <tr ng-repeat="rid in graph.getNode('${this.id}').getReasons('${this.id}')">
        <td>{{graph.getNode(rid).getText()}}</td>
        <td ng-init="graph.getNode(rid).frequency = graph.getNode(rid).defaultFrequency();">
        <input  
          ng-model="graph.getNode(rid).frequency['${this.id}']" 
          data-ng-model-options="{updateOn:'default blur',debounce:{default:5000,blur:0}}">
        </td>
        </tr>
    </tbody>
    </table>
    </div>
 </div>
 
 <div class="form-group" ng-if="graph.getNode('${this.id}').$is_sub_graph3">
    <label class="col-sm-2 control-label">关联到的运行建议</label>
    <p class="col-sm-10 form-control-static">{{graph.getNode('${this.id}').getMaxIssueString(graph)}}</p>
</div>

<br>
 <div class="form-group" ng-if="graph.getNode('${this.id}').$is_sub_graph4" >
    <label class="col-sm-2 control-label">对解决方案的置信度</label>
    <div class="col-sm-10">
    <table class="table table-responsive table-bordered">
    <thead>
        <tr><td>解决方案</td><td>置信度</td></tr>
    </thead>
    <tbody>
        <tr ng-repeat="rid in graph.getNode('${this.id}').getSolutions('${this.id}')">
        <td>{{graph.getNode(rid).getText()}}</td>
        <td ng-init="graph.getNode(rid).frequency = graph.getNode(rid).defaultFrequency();">
        <input  
          ng-model="graph.getNode(rid).frequency['${this.id}']" 
          data-ng-model-options="{updateOn:'default blur',debounce:{default:5000,blur:0}}">
        </td>
        </tr>
    </tbody>
    </table>
    </div>
</div>
 
 <div class="form-group">
    <label class="col-sm-2 control-label">备注信息</label>
    <div class="col-sm-10">
    <textarea rows="3" class="form-control" data-ng-model="graph.getNode('${this.id}').description" placeholder="请输入备注信息"></textarea>
    </div>
    <p class="col-sm-offset-2 col-sm-10 form-control-static help-block">
        <a class="btn" href="javascript:;" ng-click="removeEdge(${edge})">删除当前节点</a></label>
        <a class="btn" data-ng-if="allowFeature('${this.id}')" data-ng-click="attachFeature('${this.id}')">添加特征</a>
        <a class="btn" data-ng-if="allowFeature('${this.id}')" data-ng-click="attachGroup('${this.id}','FeatureGroup')">添加特征组</a>
         <a class="button btn" data-ng-if="allowFeature('${this.id}')" data-ng-click="attachExternalFeature('${this.id}')">添加已存在特征</a>
        <a class="button btn" data-ng-if="allowFeature('${this.id}')" data-ng-click="attachExternalGroup('${this.id}','FeatureGroup')">添加已存在特征组</a>
        <a class="btn" data-ng-if="allowCause('${this.id}')" data-ng-click="attachReason('${this.id}')">添加原因</a>
        <a class="btn" data-ng-if="allowCause('${this.id}')" data-ng-click="attachGroup('${this.id}','ReasonGroup')">添加原因组</a>
        <a class="btn" data-ng-if="allowComponent('${this.id}')" data-ng-click="attachComponent('${this.id}')">添加元件</a>
        <a class="btn" data-ng-if="allowComponent('${this.id}')" data-ng-click="attachGroup('${this.id}','ComponentGroup')">添加元件组</a>
    </p>
 </div>`;
    }
}
